<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) Wang Hongbin <wanghongbin@ngoos.org>, 极益科技
 *
 * @Author: Wang Hongbin
 * @Email: wanghongbin@ngoos.org
 * @Date:   2018-02-24 15:21:40
 * @Last Modified by:   Wang Hongbin
 * @Last Modified time: 2018-02-24 15:36:52
 ***************************************************************/
 
 /**
 * 获取bing首页大图保存在本地
 */
class GetBingPic {
	
	private $bingApiXml;
  	private $bingApiJson;

	function __construct()
	{
		// 返回xml格式
		$this->bingApiXml = "http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1";

		// 返回json格式
		$this->bingApiJson = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
	}

	/**
	 * xml格式数据
	 * @return [type] [description]
	 */
	public function getDataXml()
	{
		$url = $this->bingApiXml;
		$data = $this->httpGet($url);
		return $data;
	}

	/**
	 * json格式数据
	 * @return [type] [description]
	 */
	public function getDataXml()
	{
		$url = $this->bingApiJson;
		$data = $this->httpGet($url);
		return $data;
	}

	private function httpGet($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($curl, CURLOPT_URL, $url);

		$res = curl_exec($curl);
		curl_close($curl);

		return $res;
	}
}

$BingPic = new GetBingPic();
$xmldata = $BingPic->getDataXml();
var_dump($xmldata);
